package com.sgu6.sgu6_machine.adapter;

import android.graphics.Color;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.dto.MachineCmdDto;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MachineCmdAdapter extends RecyclerView.Adapter {

    List<MachineCmdDto> cmdDtos;

    public MachineCmdAdapter(List<MachineCmdDto> cmdDtos) {
        this.cmdDtos = cmdDtos;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_machine_cmd, viewGroup, false);
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof Holder) {
            if (cmdDtos.get(i).type == 0) {
                ((Holder) viewHolder).tvCmdType.setText("<-");
                ((Holder) viewHolder).tvCmdType.setTextColor(Color.RED);
            } else {
                ((Holder) viewHolder).tvCmdType.setText("->");
                ((Holder) viewHolder).tvCmdType.setTextColor(Color.GREEN);
            }
            ((Holder) viewHolder).tvCmd.setText("xxxxxx"+cmdDtos.get(i).cmd.substring(6));
            ((Holder) viewHolder).tvCmdDesc.setText(cmdDtos.get(i).description);

            if (i == cmdDtos.size() - 1) {
                viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
            } else {
                viewHolder.itemView.setBackground(null);
            }
        }
    }

    @Override
    public int getItemCount() {
        return cmdDtos.size();
    }

    static class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.tvCmdType)
        TextView tvCmdType;
        @BindView(R.id.tvCmdDesc)
        TextView tvCmdDesc;
        @BindView(R.id.tvCmd)
        TextView tvCmd;

        Holder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
